﻿using System.Collections.Generic;
using System.IO;

namespace CSV_Reader
{
    public static class CSV_Reader
    {
        public static IEnumerable<string[]> Parse_CSV_using_yield(string filePath, char csvSeparator)
        {
            if (csvSeparator != ',')
                yield break;
           
            using (StreamReader filetoParse = File.OpenText(filePath))
            {
                    string line;
                    while ((line = filetoParse.ReadLine()) != null)
                        yield return line.Split(csvSeparator);
            }
        }
          
        public static IEnumerable<string[]> Parse_CSV_using_list(string filePath, char csvSeparator)
        {
            if ((csvSeparator != ',') || filePath == null || !File.Exists(filePath))
                return null;

            IList<string[]> parsedInformation = new List<string[]>();

            using (StreamReader filetoParse = File.OpenText(filePath))
            {
                string line;
                while ((line = filetoParse.ReadLine()) != null)
                    parsedInformation.Add(line.Split(','));
            }
            return parsedInformation;
        }
    }
}